package exp;

import utils.BasicJPanel;
import utils.VFlowLayout;

import javax.swing.*;
import javax.swing.border.TitledBorder;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.nio.file.Path;

public class ShareJPanel extends BasicJPanel implements ChangeListener
{
    private final JLabel imageJLabel;
    private final JLabel grayJLabel;

    private final JSpinner xJSpinner;
    private final JSpinner yJSpinner;

    public ShareJPanel()
    {
        super();
        setLayout(new BorderLayout());
        {
            JPanel jPanel=new JPanel();
            add(jPanel);
            jPanel.setLayout(new GridLayout(1,2));
            jPanel.add(imageJLabel=new JLabel());
            jPanel.add(grayJLabel=new JLabel());
        }

        JPanel infoJPanel=new JPanel();
        infoJPanel.setBorder(new TitledBorder("操作"));
        JScrollPane jScrollPane=new JScrollPane(infoJPanel);
        jScrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
        add(jScrollPane,BorderLayout.WEST);
        infoJPanel.setLayout(new VFlowLayout());
        {
            JPanel jPanel=new JPanel();
            infoJPanel.add(jPanel);
            jPanel.setLayout(new FlowLayout(FlowLayout.LEFT));
            jPanel.add(new JLabel("xBias:"));
            xJSpinner=new JSpinner(new SpinnerNumberModel(0,-1,1,0.01));
            jPanel.add(xJSpinner);
            xJSpinner.addChangeListener(this);
        }
        {
            JPanel jPanel=new JPanel();
            infoJPanel.add(jPanel);
            jPanel.setLayout(new FlowLayout(FlowLayout.LEFT));
            jPanel.add(new JLabel("yBias:"));
            yJSpinner=new JSpinner(new SpinnerNumberModel(0,-1,1,0.01));
            jPanel.add(yJSpinner);
            yJSpinner.addChangeListener(this);
        }
    }

    private void update()
    {
        final BufferedImage src=(BufferedImage)((ImageIcon)imageJLabel.getIcon()).getImage();
        final double kx=(Double)xJSpinner.getValue();
        final double ky=(Double)yJSpinner.getValue();
        final int dx=(int)(kx>=0?Math.ceil(src.getHeight()*kx):Math.floor(src.getHeight()*kx));
        final int dy=(int)(ky>=0?Math.ceil(src.getWidth()*ky):Math.floor(src.getWidth()*ky));
        final BufferedImage dst=new BufferedImage(src.getWidth()+Math.abs(dx),src.getHeight()+Math.abs(dy),BufferedImage.TYPE_INT_RGB);
        final int stX=dx<0?-dx:0;
        final int stY=dy<0?-dy:0;

        for(int i=0;i<src.getHeight();++i)
            for(int j=0;j<src.getWidth();++j)
                dst.setRGB((int)(j+i*kx+stX),(int)(i+j*ky+stY),src.getRGB(j,i));

        grayJLabel.setIcon(new ImageIcon(dst));

    }

    public void update(Path path) throws IOException, BmpStructureErrorException
    {
        final Bmp bmp=new Bmp(path);
        imageJLabel.setIcon(new ImageIcon(bmp.getImage()));
        update();
    }

    public String getButtonName()
    {
        return "第2章选做3：图片错切";
    }


    @Override
    public void stateChanged(ChangeEvent e)
    {
        update();
    }
}
